//
//  ZLCalenderView.m
//  项目三
//
//  Created by rimi on 15/10/15.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "ZLCalenderView.h"
#import "ZLNoSpacingFlowLayout.h"
#import "ZLCalenderCell.h"
#import "ZLCalenderHeaderView.h"
#import "Tool.h"

static NSString * const cellIdentifier = @"daycell";
static NSString * const headerIdentifier = @"headView";

@interface ZLCalenderView ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    Tool *_tool;
    NSUInteger _cellSide;
    NSUInteger _firstWeekdayInWeek;
    NSUInteger _days;
}

@property (nonatomic, strong) UICollectionView *calenderView /**<  */;


@property (nonatomic, strong)UIView *containerView;

- (void) initializeDataSource; /**< 初始化数据源 */
- (void) initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ZLCalenderView


- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        _cellSide = frame.size.width / 7; // Cell边长
        [self initializeDataSource];
        [self initializeUserInterface];
    }
    return self;
}

- (void)initializeDataSource {
    _tool = [Tool new];
    _firstWeekdayInWeek = [_tool getWeekOfFirstDayOfMonth:[_tool getYear] withMonth:[_tool getMonth]];
    _days = [_tool getDaysOfMonth:[_tool getYear] withMonth:[_tool getMonth]];
    
}

- (void)initializeUserInterface {
    
    [self addSubview:self.calenderView];
}



#pragma mark - protocol
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _days + _firstWeekdayInWeek;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZLCalenderCell *cell;
    
    if (indexPath.row < _firstWeekdayInWeek - 1) {
        
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        
    } else {
        
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.day.text = [NSString stringWithFormat:@"%u", indexPath.row - _firstWeekdayInWeek + 2];
    }
    
    if (indexPath.row > _days + _firstWeekdayInWeek - 2) {
        cell.day.text = @"";
        
        CGRect frame = collectionView.frame;
        NSUInteger row = collectionView.visibleCells.count % 7 != 0 ? collectionView.visibleCells.count / 7 + 2 : collectionView.visibleCells.count / 7 + 1;
        frame.size.height = _cellSide * row;
        _calenderView.frame = frame;
        [self.delegate layoutCompliteWithHeight:(CGRectGetHeight(frame))];
    }
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    cell.layer.borderWidth = 1.3;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        
        ZLCalenderHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
        CALayer *layer = [CALayer layer];
        layer.bounds = header.bounds;
        layer.position = header.center;
        layer.contents = (__bridge id)([UIImage imageNamed:@"日历-2"]).CGImage;
        [header.layer insertSublayer:layer atIndex:0];
        reusableview = header;
    }
    
    return reusableview;
}

- (UICollectionView *)calenderView {
    if (!_calenderView) {
        ZLNoSpacingFlowLayout *flowLayout = [[ZLNoSpacingFlowLayout alloc]init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(_cellSide, _cellSide);
        flowLayout.headerReferenceSize = CGSizeMake(_cellSide * 7 - 2, _cellSide);
        _calenderView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
        [_calenderView registerClass:[ZLCalenderCell class] forCellWithReuseIdentifier:cellIdentifier];
        UINib *nib = [UINib nibWithNibName:@"ZLCalenderHeaderView" bundle:nil];
        [_calenderView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
        _calenderView.backgroundColor = [UIColor clearColor];
        _calenderView.delegate = self;
        _calenderView.dataSource = self;
    }
    return _calenderView;
}
@end
